package com.njupt.sellsec.service.Impl;

import com.njupt.sellsec.dao.SellUserDao;
import com.njupt.sellsec.domain.SellUser;
import com.njupt.sellsec.redis.RedisService;
import com.njupt.sellsec.redis.SellUserKey;
import com.njupt.sellsec.redis.UserKey;
import com.njupt.sellsec.result.CodeMsg;
import com.njupt.sellsec.service.SellUserService;
import com.njupt.sellsec.util.CookieUtil;
import com.njupt.sellsec.util.MD5Util;
import com.njupt.sellsec.util.UUIDUtil;
import com.njupt.sellsec.vo.UserFormVo;
import com.sun.org.apache.bcel.internal.classfile.Code;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;

/**
 * @Author: XJL
 * @Description:
 * @Date: Create in 13:31 2018/7/28
 * @Modified By:
 **/
@Service
@Slf4j
public class SellUserServiceImpl implements SellUserService {
    @Autowired
    private SellUserDao sellUserDao;
    @Autowired
    private RedisService redisService;

    public static final String COOKIE_NAME_TOKEN = "token";

    /**
     * 根据用户id获取用户信息，如果有则返回，否则查询数据库并将其设置到缓存
     * @param id
     * @return
     */
    @Override
    public SellUser findById(Long id) {

        SellUser sellUser = redisService.get(SellUserKey.getById,""+id,SellUser.class);

        if (sellUser!=null){
            return sellUser;
        }
        sellUser = sellUserDao.getSellUserById(id);
        if (sellUser!=null){

            //查询用户时根据用户id设置缓存
            redisService.set(SellUserKey.getById,""+id,sellUser);
        }
        return sellUser;
    }

    /**
     * 登陆登陆时验证已经注册过的用户信息，并将信息保存到缓存
     * @param response
     * @param userFormVo
     * @return
     */
    //CodeMsg
    public CodeMsg login(HttpServletResponse response,
                         UserFormVo userFormVo){
        if (userFormVo==null){
            return CodeMsg.SERVER_ERROR;
        }
        String mobile = userFormVo.getMobile();
        String password = userFormVo.getPassword();
        SellUser sellUser = sellUserDao.getSellUserById(Long.parseLong(userFormVo.getMobile()));
        if (sellUser==null){
            return CodeMsg.MOBILE_NOT_EXIST;
        }
        String salt = sellUser.getSalt();
        String checkPass = MD5Util.formPassToDBPass(userFormVo.getPassword(),salt);
        log.info("【密码校验】,pass={}",checkPass);
        if (!checkPass.equals(sellUser.getPassword())){
            return CodeMsg.PASSWORD_ERROR;
        }

        String token = UUIDUtil.genUUID();

        //登陆时，根据生成的token设置缓存
        redisService.set(SellUserKey.token,token,sellUser);
        CookieUtil.setCookie(response,COOKIE_NAME_TOKEN,token,SellUserKey.token.expireSeconds());


        return CodeMsg.SUCCESS;
    }

    /**
     * 从缓存中获取User信息，并更新过期时间
     * @param response
     * @param token
     * @return
     */
    @Override
    public SellUser findUserByToken(HttpServletResponse response,String token) {
        if (StringUtils.isEmpty(token)){
            return null;
        }else {
            SellUser sellUser = redisService.get(SellUserKey.token,token,SellUser.class);
            if (sellUser!=null){
                redisService.set(SellUserKey.token,token,sellUser);
                CookieUtil.setCookie(response,COOKIE_NAME_TOKEN,token,SellUserKey.token.expireSeconds());
            }
            return sellUser;
        }

    }


    /**
     * 更新用户password，首先查用户信息，设置更新数据，保存至数据库，并更新缓存数据。
     * @param token
     * @param id
     * @param formPass
     * @return
     */
    @Override
    public boolean updatePassword(String token,long id,String formPass){

        //查询用户是否存在
        SellUser sellUser = findById(id);
        if (sellUser==null){
            return false;
        }
        //设置新的password
        SellUser updateUser = new SellUser();
        String mdPass = MD5Util.formPassToDBPass(formPass,sellUser.getSalt());
        updateUser.setPassword(mdPass);
        //将password写入数据库
        sellUserDao.updatePass(updateUser);

        //更新缓存中的数据
        boolean resu = redisService.del(SellUserKey.getById,""+id);
        sellUser.setPassword(mdPass);
        redisService.set(SellUserKey.getById,""+id,sellUser);
        //更新缓存中用户登陆的token信息
        boolean reu = redisService.set(SellUserKey.token,token,sellUser);

        return true;



    }

}
